package com.drondea.testclient.handler.sgip;

import cn.hutool.core.util.StrUtil;
import com.drondea.sms.message.sgip12.SgipSubmitResponseMessage;
import com.drondea.testclient.handler.AbstractSubmitResponseHandler;
import com.drondea.testclient.panel.SgipPanel;
import com.drondea.testclient.util.counter.SgipAtomicUtil;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * 测试收到的短信回应
 *
 * @author 27581
 */
@Slf4j
@Sharable
public class SgipSubmitResponseHandler extends AbstractSubmitResponseHandler<SgipSubmitResponseMessage> {

    private static final SgipPanel PANEL = SgipPanel.getInstance();

    @Override
    public void channelRead0(final ChannelHandlerContext ctx, SgipSubmitResponseMessage msg) {
        log.debug("SgipSubmitResponseMessage : {} : result: {}", msg.getSequenceId(), msg.getResult());
        PANEL.responseCountLabel.setText(StrUtil.format("响应数量: {}", SgipAtomicUtil.RESPONSE_COUNT.incrementAndGet()));
        short result = msg.getResult();
        if (result == 0) {
            PANEL.successCountLabel.setText(StrUtil.format("提交成功: {}", SgipAtomicUtil.SUCCESS_COUNT.incrementAndGet()));
        } else {
            PANEL.failCountLabel.setText(StrUtil.format("提交失败: {}", SgipAtomicUtil.FAIL_COUNT.incrementAndGet()));
        }

        // 当响应数量=发送数量时，设置结束时间
        if (SgipAtomicUtil.SEND_COUNT.get() == SgipAtomicUtil.RESPONSE_COUNT.get()) {
            calAvgSubmitSpeed(PANEL, SgipAtomicUtil.SEND_COUNT.get() - PANEL.submitCountBefSend);
        }
    }

}
